package com.xyz.mycamera

import android.Manifest
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.graphics.Color
import android.os.Bundle
import android.view.WindowManager
import android.view.animation.CycleInterpolator
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.blankj.utilcode.util.PermissionUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.xyz.mycamera.databinding.ActivityVideoBinding
import com.xyz.mycamera.picture.PictureSelectorUtils
import xyz.doikki.videocontroller.StandardVideoController


class VideoActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityVideoBinding.inflate(layoutInflater)
    }

    private val permission by lazy {
        arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        setContentView(binding.root)
        ImmersionBar.with(this)
            .fullScreen(true)
            .hideBar(BarHide.FLAG_HIDE_BAR)
            .init()

        if (PermissionUtils.isGranted(*permission)) {
            binding.surfaceView.switchCamera()
            binding.surfaceView.startCamera()
        } else {
            PermissionUtils.permission(
                *permission
            ).callback { isAllGranted, _, _, _ ->
                if (isAllGranted) {
                    binding.surfaceView.startCamera()
                }
            }.request()
        }

        binding.time.setShadowLayer(5f, 2f, 2f, Color.BLUE)
        binding.btnChoose.setOnClickListener {
            PictureSelectorUtils.openGalleryVideo(
                this, arrayListOf(), 1, null
            ) { b, list ->
                if (list.size > 0) {
                    play(list[0].path)
                    binding.btnChoose.isVisible = false
                }
            }
        }
        ObjectAnimator.ofFloat(binding.fegChe,"rotation", 0f, 360f).apply {
            duration = 2000
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            start()
        }
        ObjectAnimator.ofFloat(binding.janTou,"translationY", 0f, 50f).apply {
            duration = 2000
            repeatCount = ValueAnimator.INFINITE
            interpolator = CycleInterpolator(0.5f)
            start()
        }
    }

    private fun play(url: String) {
        binding.videoView.setUrl(url) //设置视频地址
        val controller = StandardVideoController(this)
        controller.addDefaultControlComponent("", true)
        binding.videoView.setVideoController(controller) //设置控制器
        binding.videoView.setLooping(true)
        binding.videoView.start() //开始播放，不调用则不自动播放
    }

    override fun onPause() {
        super.onPause()
        binding.videoView.pause()
    }

    override fun onResume() {
        super.onResume()
        binding.videoView.resume()
        binding.surfaceView.startCamera()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.videoView.release()
        binding.surfaceView.onDestroy()
    }

    override fun onBackPressed() {
        if (!binding.videoView.onBackPressed()) {
            super.onBackPressed()
        }
    }
}